package pers.cz.common.connect;

import pers.cz.common.connect.localfile.LocalFileConnection;
import pers.cz.common.connect.simpledb.DbConnnection;

import javax.sql.DataSource;
import java.io.File;

/**
 * @program: postgirl-collection-client
 * @description: 抽象连接工厂
 * @author: Cheng Zhi
 * @create: 2022-07-31 11:59
 **/
public abstract class ConnectionFactory implements AutoCloseable{

    public abstract Object getSource();

    private IConnection createConnection(StoreType type) {
        IConnection connection = null;

        switch (type) {
            case SIMPLEDB:
                // 简单数据库操作
                DataSource dataSource = (DataSource) getSource();
                connection = new DbConnnection(dataSource);
                break;
            case LOCALFILE:

                connection = new LocalFileConnection((File) getSource());
                break;
            default:
                break;
        }

        return connection;
    }

    public IConnection getConnection(StoreType type) {

        IConnection connection = createConnection(type);

        return connection;
    }

    @Override
    public void close() throws Exception {

    }
}
